import {defineStore, mapActions} from 'pinia'
import {ref,toRef, toRefs} from 'vue'
//1.模块名
//2.函数（定义数据很方法）

const usePcsStore = defineStore("pcs",()=>{
    //定义数据
    let datamap = new Map()
    let pcsyx=toRef({
        name:"pcs1",
        type1:"yx",
        data:[]
    })
    let pcsyc=toRef({
        name:"pcs1",
        type1:"yc",
        data:[]
    })
    let pcsyt=toRef({name:"pcs1",
        type1:"yt",
        data:[]})
    let pcsyk=toRef({name:"pcs1",
        type1:"yk",
        data:[]})

    let bmsyx=toRef({
        name:"bms1",
        type1:"yx",
        data:[]
    })
    let bmsyc=toRef({
        name:"bms1",
        type1:"yc",
        data:[]
    })
    let bmsyt=toRef({name:"bms1",
        type1:"yt",
        data:[]})
    let bmsyk=toRef({name:"bms1",
        type1:"yk",
        data:[]})




    const upGradeDev=(data:any)=>{
       let find =  false
       let has = false
        switch(data.dev_type){
            case "pcs":{
               find = true;
               break;
            }
            case "bms":{
                find = true;
                break;}
            default:{
                find = false
            }
   
        }
        if (find == false) {

        }else{
             has = datamap.has(data.dev_type)
            if (has == false) {
                datamap.set(data.dev_type,new Map())
                datamap.get(data.dev_type).set(data.dev_name,new Map())
            }else{
                has = datamap.get(data.dev_type).has(data.dev_name)
                if (has == false){
                    datamap.get(data.dev_type).set(data.dev_name,new Map())
                }
            }
          switch(data.para_type){
            case "yx":{
                find =true
              break;
            }
            case "yc":{
                find =true
                break;
            }
            case "yk":{
                find =true
                break;
            }
            case "yt":{
                find =true
                break;
            }
            default:
                find = false
          }
          if (find == true){
            has = datamap.get(data.dev_type).get(data.dev_name).has(data.para_type)
            if (false == has)
                {
                    datamap.get(data.dev_type).get(data.dev_name).set(data.para_type,[])
                }
                let  dataobj = datamap.get(data.dev_type).get(data.dev_name).get(data.para_type)
                let  oldmap = new Map()
                dataobj.forEach((val,index)=>{
                        oldmap.set(val.key,val)
                })       
                data.data.forEach((val,index)=>{
                    oldmap.set(val.key,val)
                })
                dataobj=[]
                oldmap.forEach((value,key)=>{
                    dataobj.push(value)
                })
                dataobj.sort((a,b) => a.reg_addr-b.reg_addr)
                datamap.get(data.dev_type).get(data.dev_name).set(data.para_type,dataobj)
                switch(data.dev_type){
                    case "pcs":{
                        switch (data.para_type){
                            case "yx":{
                                pcsyx.value.data = dataobj
                              break;
                            }
                            case "yc":{
                                pcsyc.value.data = dataobj
                                break;
                            }
                            case "yk":{
                                pcsyk.value.data = dataobj
                                break;
                            }
                            case "yt":{
                                pcsyt.value.data = dataobj
                                break;
                            }
                       
                        }
                        break;
                     }
                     case "bms":{
                        switch (data.para_type){
                            case "yx":{
                                bmsyx.value.data=dataobj
                              break;
                            }
                            case "yc":{
                                bmsyc.value.data = dataobj
                                break;
                            }
                            case "yk":{
                                bmsyk.value.data = dataobj
                                break;
                            }
                            case "yt":{
                                bmsyt.value.data = dataobj
                                break;
                            }
                     
                        }
                         break;}
  
                }
          }
        }

    }
    return{
        pcsyx,pcsyc,pcsyt,pcsyk,bmsyx,bmsyc,bmsyt,bmsyk,upGradeDev
    }
})

export default usePcsStore